﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using UsbBitWacker;

namespace UbwApplication
{
    public partial class MainForm : Form
    {
        UBW ubw;
        UbwDdeServer ddeServer;

        public MainForm()
        {
            InitializeComponent();

            this.Text += " - v" + this.ProductVersion;
        }

        internal void ReportException(Exception ex)
        {
            MessageBox.Show(ex.ToString(), ex.GetType().Name);
        }

        #region Initialization Controls

        private void MainForm_Shown(object sender, EventArgs e)
        {
            try
            {
                // Just connect automatically
                var cmdLineArgs = Environment.GetCommandLineArgs();
                if (cmdLineArgs.Any(c => c.Equals("connect", StringComparison.CurrentCultureIgnoreCase)))
                {
                    Connect();
                    // minimize to tray.
                    this.WindowState = FormWindowState.Minimized;
                }
            }
            catch (Exception ex)
            {
                if (ubw != null)
                    ubw.Close();
                
                InitGroupBox.Enabled = true;
            }
        }

        private void ucConnect_Click(object sender, EventArgs e)
        {
            try
            {
                Connect();
            }
            catch (Exception ex)
            {
                if (ubw != null)
                    ubw.Close();

                ReportException(ex);
                InitGroupBox.Enabled = true;
            }
        }

        private void Connect()
        {
            InitGroupBox.Enabled = false;

            if (uxComPort.Text.Equals("AUTO", StringComparison.CurrentCultureIgnoreCase))
            {
                uxComPort.Text = UsbBitWacker.UBW.FindUbwComPort();
            }

            ubw = new UBW();
            if (ubw.Open(uxComPort.Text) == 0)
            {
                throw new ApplicationException(ubw.GetErrorString());
            }

            ucConnect.Enabled = false;

            // All ports as inputs, with however many analog inputs as configured
            ubw.ConfigureIO(0xFF, 0xFF, 0xFF, (int)uxAnalogInputCount.Value);

            if (ubw != null)
            {
                ddeServer = new UbwDdeServer(ubw);
                ddeServer.Register();
            }
        }

        #endregion

        private UbwDiagramForm ubwDiagramForm = null;
        private void ucShowUbwDiagram_Click(object sender, EventArgs e)
        {
            if (ubwDiagramForm == null || ubwDiagramForm.IsDisposed)
            {
                ubwDiagramForm = new UbwDiagramForm();
                ubwDiagramForm.Show(this);
            }
        }

        private UbwDdeServerHelp ubwDdeServerHelpForm = null;
        private void ucShowDdeCommands_Click(object sender, EventArgs e)
        {
            if (ubwDdeServerHelpForm == null || ubwDdeServerHelpForm.IsDisposed)
            {
                ubwDdeServerHelpForm = new UbwDdeServerHelp();
                ubwDdeServerHelpForm.Show(this);
            }
        }

        #region Tray Icon Support

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.Activate();
            }
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            }
            else
            {
                this.ShowInTaskbar = true;
            }
        }

        #endregion
    }
}
